﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CITWebsite.Models;
using DTO.Models;
using System.IO;
using CITWebsite.Helper;

namespace CITWebsite.Controllers
{
    public class FeedbackController : Controller
    {
        //
        // GET: Index
        [Authorize]
        public ActionResult Index(string id)
        {
            MEDIA obj = null;
            if (string.IsNullOrEmpty(id))
                return View(obj);

            DataRepository dr = new DataRepository();
            MEDIA[] arr = dr.MediaProvider.getMedia(id).ToArray();
            if (arr.Length >= 1)
                obj = arr[0];
            obj.Content = System.Web.HttpUtility.HtmlDecode(obj.Content);
            return View(obj);
        }



        [Authorize]
        [HttpPost]
        public ActionResult Index(MEDIA obj)
        {
            DataRepository dr = null;

            if (!ModelState.IsValid)
                return View(obj);
            else
            {
                dr = new DataRepository();
                obj.UserModify = User.Identity.Name;
                obj.DateModify = DateTime.Now;
                dr.MediaProvider.Update(obj);

                //upload image
                HttpPostedFileBase file = Request.Files[0];
                if (file.ContentLength > 0)
                {
                    string filePath = Path.Combine(HttpContext.Server.MapPath("~/MediaImage"), obj.ID.ToString() + ".jpg");
                    file.SaveAs(filePath);
                }

                return RedirectToAction("Index", new { id = obj.TypeMediaName });
            }
        }

        [Authorize]
        public ActionResult FeedbackList()
        {
            return View();
        }

        [Authorize]
        [HttpGet]
        public ActionResult List(string id, string pPageIndex, string email, string pDateFrom, string pDateTo, string pIsAnswer)
        {
            int PageSize = Common.pagesize;
            int PageIndex = Convert.ToInt32(pPageIndex);

            DateTime DateFrom = pDateFrom == null ? Common.datemin : Common.ChangeDateVNToUS(pDateFrom);
            DateTime DateTo = pDateTo == null ? Common.datemax : Common.ChangeDateVNToUS(pDateTo);

            DataRepository dr = new DataRepository();

            IQueryable<FEEDBACK> source = null;
            if (pIsAnswer == null)
                source = dr.FeedbackProvider.GetAll(email != null ? email : string.Empty, DateFrom, DateTo);
            else
                source = dr.FeedbackProvider.GetAll(email != null ? email : string.Empty, DateFrom, DateTo, pIsAnswer == "0" ? true : false);

            var model = source.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToArray();

            int len = source.ToList<FEEDBACK>().Count;
            ViewData["TotalPage"] = (len % PageSize == 0 ? len / PageSize : len / PageSize + 1).ToString();
            ViewData["PageIndex"] = PageIndex.ToString();
            ViewData["PageSize"] = PageSize.ToString();
            ViewData["TypeMediaName"] = id;

            return View(model);
        }

        [Authorize]
        public ActionResult Delete(string id)
        {
            DataRepository dr = new DataRepository();
            dr.FeedbackProvider.DeleteMark(id);

            return Content("");
        }

        [Authorize]
        public ActionResult Answer(string id)
        {
            DataRepository dr = new DataRepository();
            dr.FeedbackProvider.Answer(int.Parse(id));
            
            return Content("");
        }
    }
}
